//package com.sensorfriendly.ui;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Locale;
//
//import android.app.Activity;
//import android.content.Context;
//import android.content.Intent;
//import android.content.pm.PackageManager;
//import android.content.pm.ResolveInfo;
//import android.net.ConnectivityManager;
//import android.net.NetworkInfo;
//import android.os.Bundle;
//import android.speech.RecognizerIntent;
//import android.speech.tts.TextToSpeech;
//import android.speech.tts.TextToSpeech.OnInitListener;
//import android.view.GestureDetector;
//import android.view.MotionEvent;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.view.View.OnTouchListener;
//import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
//import android.widget.Button;
//import android.widget.GridView;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import com.sensorfriendly.AppConstants;
//import com.sensorfriendly.AppContext;
//import com.sensorfriendly.R;
//
//public class NavigationActivity extends Activity implements OnClickListener, OnInitListener{
//
//	private static final int VTT_CHECK = 0;
//	private static final int TTS_CHECK = VTT_CHECK + 1;
//	
//	private GestureDetector gesturedetector = null;
//	private View layout;
//	private GridView gridView;
//	private List<String> currentOptions;
//	private TextToSpeech myTTS = null;
//
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//
//		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_navigation);
//		
//		AppContext app = (AppContext) getApplication();
//		int pageNum;
//		Bundle extra = this.getIntent().getExtras();
//		if(extra == null){
//			pageNum = 0;
//			app.setCurrentPage(pageNum);
//		}
//		else{
//			pageNum = extra.getInt(AppConstants.PAGE_NUM_KEY);
//		}
//		currentOptions = app.getPageOptions().get(pageNum);
//		
//		handleGridView();
//		handleGestureListner();
//		
//		PackageManager pm = getPackageManager();
//		List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
//				RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
//
//		Button speakButton = (Button) findViewById(R.id.speak);
//		if (activities.size() == 0 || !isOnline()) {
//			speakButton.setEnabled(false);
//			speakButton.setVisibility(View.GONE);
//		} else {
//			speakButton.setOnClickListener(this);
//		}
//	}
//	
//	@Override
//	protected void onResume() {
//		super.onResume();
//		if (myTTS == null) {
//			// check for TTS data
//			Intent checkTTSIntent = new Intent();
//			checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
//			startActivityForResult(checkTTSIntent, TTS_CHECK);
//		}
//	}
//	@Override
//	protected void onPause(){
//		super.onPause();
//		if(myTTS != null){
//			myTTS.stop();
//		}
//	}
//
//	@Override
//	public void onDestroy() {
//		if (myTTS != null) {
//			myTTS.shutdown();
//		}
//		super.onDestroy();
//		System.gc();
//	}
//
//	public boolean dispatchTouchEvent(MotionEvent ev) {
//		super.dispatchTouchEvent(ev);
//		return gesturedetector.onTouchEvent(ev);
//	}
//	
//	/*-------------------------------------*
//	 * This function is used to setup the  *
//	 * Listener to detect "touch" events   *
//	 *-------------------------------------*/
//	private void handleGestureListner(){
//		layout = (LinearLayout) findViewById(R.id.root);
//
//		layout.setOnTouchListener(new OnTouchListener() {
//			public boolean onTouch(View v, MotionEvent event) {
//				gesturedetector.onTouchEvent(event);
//				return true;
//			}
//		});
//	}
//	
//	/*----------------------------------------------------------------------*
//	 * This function is responsible for populates the GridView with the     *
//	 * current Categories and sets up the Listener to detect click          *
//	 *----------------------------------------------------------------------*/
//	private void handleGridView(){
//		gesturedetector = new GestureDetector(new MyGestureListener());
//		gridView = (GridView) findViewById(R.id.categories);
//
//		gridView.setAdapter(new ImageAdapter(this, currentOptions));
//		
//		/*-------------------------------------------------*
//		 *  The following onClickListner responds to the   *
//		 *  click on one of 4 middle buttons (Grid View)   *
//		 *-------------------------------------------------*/
//		gridView.setOnItemClickListener(new OnItemClickListener() {
//			public void onItemClick(AdapterView<?> parent, View v,
//					int position, long id) {
//				navigateHandler(position,   ((TextView) v.findViewById(R.id.grid_item_label))
//				   .getText().toString().toLowerCase());
//			}
//		});
//	}
//	
//	/*--------------------------------------------*
//	 * This function calls the Message activity   *
//	 * with the category that was selected to     *
//	 * display the messages                       *
//	 *--------------------------------------------*/
//	private void navigateHandler(int id, String category){
//		//Toast.makeText(this, "ID: "+id+ " Text: "+category, Toast.LENGTH_LONG).show();
//		
//		Intent messageIntent = new Intent(NavigationActivity.this, MessageActivity.class);
//		messageIntent.putExtra(AppConstants.CATEGORY, category);
//		startActivity(messageIntent);
//	}
//	
//	
//	/*--------------------------------------*
//	 * Start the voice recognition activity *
//	 *--------------------------------------*/
//	@Override
//	public void onClick(View v) {
//		if (v.getId() == R.id.speak) {
//			startVoiceRecognitionActivity();
//		}
//	}
//	
//	/*------------------------------------*
//	 * Check if the user has an internet  *
//	 * connection required for voice      *
//	 * support                            *
//	 *------------------------------------*/
//	public boolean isOnline() {
//	    ConnectivityManager cm =
//	        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
//	    if (netInfo != null && netInfo.isConnected()) {
//	        return true;
//	    }
//	    return false;
//	}
//	
//	
//	/*----------------------------------*
//	 * Initialize and setup the voice   *
//	 * recognition                      *
//	 *----------------------------------*/
//	private void startVoiceRecognitionActivity() {
//		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
//				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//		intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
//		intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
//				"Speak the option you want to select...");
//		startActivityForResult(intent, VTT_CHECK);
//	}
//
//	/*-------------------------------------*
//	 * Handle the results from the voice   *
//	 * recognition activity.               *
//	 *-------------------------------------*/
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		if (requestCode == VTT_CHECK && resultCode == RESULT_OK) {
//			ArrayList<String> matches = data
//					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//			checkMatches(matches);
//		} else if (requestCode == TTS_CHECK) {
//			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//				myTTS = new TextToSpeech(this, this);
//			} else {
//				Toast.makeText(this, "Speech data not available",
//						Toast.LENGTH_LONG).show();
//			}
//		}
//		super.onActivityResult(requestCode, resultCode, data);
//	}
//	
//	/*----------------------------------------*
//	 * Checks the best match of the voice     *
//	 * input and sends it to the Navigation   *
//	 * Handler                                *
//	 *----------------------------------------*/
//	private void checkMatches(List<String> matches) {
//		int optionIndex=0;
//		for(String option : currentOptions){
//			for(String match : matches){
//				System.out.println(match);
//				if(match.toLowerCase().contains(option.toLowerCase())){
//					navigateHandler(optionIndex, option.toLowerCase());
//					return;
//				}
//			}
//			optionIndex++;
//		}
//	}
//
//	// setup TTS
//	public void onInit(int initStatus) {
//		// check for successful instantiation
//		if (initStatus == TextToSpeech.SUCCESS) {
//			if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
//				myTTS.setLanguage(Locale.US);
//			speakOptions();
//		} else if (initStatus == TextToSpeech.ERROR) {
//			Toast.makeText(this, "Sorry! Text To Speech failed...",
//					Toast.LENGTH_LONG).show();
//		}
//	}
//	
//	/*-----------------------------------------*
//	 *  Speaks whatever strings are sent to it *
//	 *-----------------------------------------*/
//	private void speakOptions() {
//		StringBuilder sb = new StringBuilder();
//		sb.append("How can I help you? You can say, ");
//		for(String option:currentOptions){
//			sb.append(option);
//			sb.append(", ");
//		}
//		sb.append("or,  ");
//		sb.append( "other, ");
//		sb.append("for more options");
//		myTTS.speak(sb.toString(), TextToSpeech.QUEUE_FLUSH, null);
//	}
//	
//	/*---------------------------------------------*
//	 * The Gesture class that detects the gesture  *
//	 * and determines if it is appropriate enough  *
//	 * to be considered a left/right swipe         *
//	 *---------------------------------------------*/
//	class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
//
//		private static final int SWIPE_MIN_DISTANCE = 30;
//		private static final int SWIPE_MAX_OFF_PATH = 250;
//		private static final int SWIPE_THRESHOLD_VELOCITY = 30;
//
//		@Override
//		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//
//			float dX = e2.getX() - e1.getX();
//			float dY = e1.getY() - e2.getY();
//
//			if (Math.abs(dY) < SWIPE_MAX_OFF_PATH 
//					&& Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY 
//					&& Math.abs(dX) >= SWIPE_MIN_DISTANCE) {
//
//				if (dX > 0) {
//					AppContext app = (AppContext)getApplication();
//					int prevPageNum = app.previousPage();
//	
//					/**** Start an activity that goes to the previous page ****/
//					Intent previousPage = new Intent(getApplicationContext(), NavigationActivity.class);
//					previousPage.putExtra(AppConstants.PAGE_NUM_KEY, prevPageNum);
//					startActivity(previousPage);
//					
//					/**** Animate the transition ****/
//					overridePendingTransition(R.anim.slide_right_incoming,R.anim.slide_right_outgoing);
//				} 
//				else {
//					AppContext app = (AppContext)getApplication();
//					int nextPageNum = app.nextPage();
//	
//					/**** Start an activity that goes to the next page ****/
//					Intent nextPage = new Intent(getApplicationContext(), NavigationActivity.class);
//					nextPage.putExtra(AppConstants.PAGE_NUM_KEY, nextPageNum);
//					startActivity(nextPage);
//
//					/**** Animate the transition ****/
//					overridePendingTransition(R.anim.slide_left_incoming,R.anim.slide_left_outgoing);
//				}
//				finish();
//				return true;
//			} 
//			return false;
//		}
//	} // End MyGestureListner class 
//	
//}
